import { _HttpClient } from '@delon/theme';
import { QueryOptions } from 'src/app/project/model/common/query-options';
import { Observable } from 'rxjs';
import { ApiPagedData } from 'src/app/project/model/common/api-paged-data';
import { ClassDTO } from 'src/app/project/model/icm/icm-base/class/class.dto';
import { ApiSimpleData } from 'src/app/project/model/common/api-simple-data';
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class ClassService {
  private static URL = '/api/icm-class';

  constructor(private http: _HttpClient) {}

  /**
   * 分页查询班级业信息
   * @param option 分页参数
   * @param code 班级编码
   * @param name 班级名称
   * @param grade 年级
   * @param academyId 所属学院
   * @param professionId 所属专业
   */
  findOnePage(
    option: QueryOptions,
    code?: string,
    name?: string,
    grade?: string,
    academyId?: string,
    professionId?: string,
  ): Observable<ApiPagedData<ClassDTO>> {
    const params = {};
    Object.assign(
      params,
      option,
      code ? { code } : {},
      name ? { name } : {},
      grade ? { grade } : {},
      academyId ? { academyId } : {},
      professionId ? { professionId } : {},
    );
    return this.http.get<ApiPagedData<ClassDTO>>(ClassService.URL, params);
  }

  /**
   * 添加班级
   * @param classDTO 班级信息
   */
  create(classDTO: ClassDTO): Observable<ClassDTO> {
    return this.http.post<ClassDTO>(ClassService.URL, classDTO);
  }

  /**
   * 根据id查找班级
   * @param id 班级id
   * @param isGetStudent  是否获取学生
   */
  findById(id: string, isGetStudent: boolean): Observable<ClassDTO> {
    const params = {};
    Object.assign(params, isGetStudent != null ? { isGetStudent } : {});
    return this.http.get<ClassDTO>(`${ClassService.URL}/${id}`, params);
  }

  /**
   * 修改更新班级信息
   * @param id 班级id
   * @param classDTO 班级信息
   */
  complete(id: string, classDTO: ClassDTO): Observable<ClassDTO> {
    return this.http.put<ClassDTO>(`${ClassService.URL}/${id}`, classDTO);
  }

  /**
   * 根据id删除班级
   * @param id 班级id
   */
  delete(id: string): Observable<ClassDTO> {
    return this.http.delete<ClassDTO>(`${ClassService.URL}/${id}`);
  }

  /**
   * 查找所有班级，可按需求排序
   * @param param 排序参数
   * @param pattern 排序模式: asc/desc
   */
  findAllBySort(param?: string, pattern?: string): Observable<Array<ClassDTO>> {
    const params = {};
    Object.assign(params, param ? { param } : {}, pattern ? { pattern } : {});
    return this.http.get<Array<ClassDTO>>(`${ClassService.URL}/all/sort`, params);
  }

  /**
   * 根据班级code，检查班级code是否已存在
   * @param professionId 专业id
   * @param code 班级code
   */
  checkoutCodeIsExist(professionId: string, code: string): Observable<ApiSimpleData<boolean>> {
    const params = {};
    Object.assign(params, code ? { code } : {});
    return this.http.get<ApiSimpleData<boolean>>(`${ClassService.URL}/checkCodeExist/${professionId}`, params);
  }

  /**
   * 统计班级数量
   */
  count(): Observable<number> {
    return this.http.get<number>(`${ClassService.URL}/count`);
  }
}
